package main

import (
	"fmt"
	"sort"
)

/**
 * @author: guangcheng
 * @date:   2020/11/18 15:31
 * @version  V1.0
 * @description:
 *
 */
func main() {
	var monsters []map[string]string
	monsters = make([]map[string]string, 2)

	if monsters[0] == nil {
		monsters[0] = make(map[string]string, 2)
		monsters[0]["name"] = "铁扇公主"
		monsters[0]["age"] = "500"
	}

	//[map[age:500 name:铁扇公主] map[]]
	fmt.Println(monsters)

	if monsters[1] == nil {
		monsters[1] = make(map[string]string, 2)
		monsters[1]["name"] = "孙悟空"
		monsters[1]["age"] = "500"
	}

	//[map[age:500 name:铁扇公主] map[age:500 name:孙悟空]]
	fmt.Println(monsters)

	//正确使用方式
	newMonster := map[string]string{
		"name": "阿宝",
		"age":  "20",
	}
	monsters = append(monsters, newMonster)
	fmt.Println(monsters)

	//map sort
	//1 key save to slice
	//2 sort for slice
	//3 遍历切片 按key输出
	map1 := make(map[int]int, 10)
	map1[10] = 100
	map1[1] = 90
	map1[4] = 80
	map1[6] = 99
	map1[3] = 63

	//map[1:90 3:63 4:80 6:99 10:100] ??? 新版本中增加了默认排序???
	fmt.Println(map1)

	var keySlice []int
	for k, _ := range map1 {
		keySlice = append(keySlice, k)
	}

	//sort
	sort.Ints(keySlice)
	fmt.Println(keySlice)

	for _, k := range keySlice {
		fmt.Printf("map1[%v]=%v", k, map1[k])
	}

}
